library LaserDesignator initializer init requires LegacySystem

	private function LaserDesignatorConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A01O' or i=='A024' or i=='A03H' or i=='A06M' or i=='A07C' or i == 'A06Q'
	endfunction

	private function LaserDesignator takes nothing returns nothing
		local unit u = GetSpellTargetUnit()
		local unit u2 = GetTriggerUnit()
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		
		local integer i = 1
		local unit missile

		local boolean echoCompanyHeroDesignator = false
		
		set missile = CreateUnit(Player(11),'h00T',GetUnitX(u2),GetUnitY(u2),0.0)
		call UnitApplyTimedLife(missile ,'Bhwd',3)
		call IssuePointOrder(missile,"attackground",x,y)
		
		
		if GetUnitAbilityLevel(u,'B009')<=0 then
			if IsUnitType(u,UNIT_TYPE_HERO)==true then
				set echoCompanyHeroDesignator = true
				call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 6.0, "|cfff3ad00" + GetPlayerName(GetOwningPlayer(u2)) + "|r : |cff1e90ff" + GetUnitName(u) + "|r has been designated." )
			else
				if IsUnitType(GetTriggerUnit(),UNIT_TYPE_HERO) then
					set echoCompanyHeroDesignator = true
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 3.0, "|cfff3ad00" + GetPlayerName(GetOwningPlayer(u2)) + "|r : " + GetUnitName(u) + " has been designated." )
				endif
			endif
		endif

		call AttachSoundToUnit(gg_snd_LaserDesignator,u)
		call SetSoundVolume(gg_snd_LaserDesignator, 127)
		call StartSound(gg_snd_LaserDesignator)

		if echoCompanyHeroDesignator then
			call SetCameraQuickPosition(x,y)
			call PingMinimapEx(x, y, 5.0, 38, 100, 242, false)
		endif

		if GetUnitAbilityLevel(u2,'A022')>3 or GetUnitAbilityLevel(u2,'A01K')>3 then //Covert Ops and Recon (Learn)
		    loop
		        exitwhen i>8 or GetWidgetLife(u)<0.405
		        call TriggerSleepAction(2.5)
		        set x = GetUnitX(u)
		        set y = GetUnitY(u)
		        //call SetCameraQuickPosition(x,y)
		        call PingMinimapEx(x, y, 1.35, 38, 115, 242, false)
		        call StartSound(gg_snd_HeartBeat)
		        set i=i+1
		    endloop
	    endif
		set u = null
		set u2 = null
		set missile = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition(function LaserDesignatorConditions))
		call TriggerAddAction(t,function LaserDesignator)
	endfunction

endlibrary